<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Ajax_Booklet extends Controller{

public function action_index()
{
}

public function action_add()
{
	$name =  arr::get($_POST, 'name');
	$description =  arr::get($_POST, 'description');
	$category_id_array =  arr::get($_POST, 'category');
	$lock = arr::get($_POST, 'lock');
	if (!$name)
	{
		echo '书名为空';
		return;
	}
	$lock_booklet = 'off';
	if ($lock)
	{
		$lock_booklet = 'on';
	}
	$booklet = ORM::factory('booklet');
	$booklet->values(array(
		'name' 		  => $name,
		'description' => $description,
		'create_time' => time(),
		'lock'		  => $lock_booklet
	));
	try
	{
		$booklet->save();
	}
	catch (ORM_Validation_Exception $e)
	{
		$errors = $e->errors('model');
		return;
	}
	Book_Booklet::instance()->save_booklet_record($booklet);
	Book_Booklet::instance()->save_category($booklet, $category_id_array);
	$result = array(
		"result" => '添加成功',
		"id"     => $booklet->id
	); 
	echo json_encode($result);
}

}




